home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / demoCDEF ƒ / About CDEF's next >
Encoding:
Text File  |  1994-07-17  |  14.8 KB  |  358 lines  |  [TEXT/KAHL]

  1. /*----------------------------------------------------------------------
  2. Revision History:
  3.  
  4. v1.0    June 1994    - first distribution
  5. v1.01    July 1994    - 3D Buttons now supports multi-line titles
  6.                     - GroupBox uses Control Max instead of RefCon for
  7.                       true height of box (templates must be changed).
  8.                     - both slider controls have a correction to tracking
  9.                       logic when mouse is moved past end of slider. Now,
  10.                       tracking will not resume until mouse is moved back
  11.                       to the thumb.
  12. ------------------------------------------------------------------------
  13. Here is a collection of 9 CDEF's (Control Definitions) and some other
  14. bits of source code to demo their use.
  15.  
  16. This stuff is also pretty much un-documented - this file and comments
  17. in the source is about it.  I've learned to appreciate the job that
  18. Tech Writers do - I've started some "real" documentation, but it
  19. never seems complete.  
  20.  
  21. Read the source code - especially the .r file - it might help you
  22. understand how to use these CDEF's.
  23.  
  24. There are no restrictions on this (as far as I know and care), use it
  25. if you wish.  If you find some use for this code, I'd love to know 
  26. about it.
  27.  
  28. This code is absolutely unwarranted.  I'm sure that there are some
  29. errors somewhere, but it does seem to work.  I'm uploading this
  30. because when I was learning (I still am…) how to program on the Mac,
  31. I learned a lot from source code that others uploaded.  Hopefully, 
  32. this will help someone else learn something about the Mac.
  33.  
  34. The source code for the actual CDEF's is not included here, but you
  35. can find the CDEF's as code resources in file xDEF.rsrc.  I'd like
  36. to see the CDEF's "shaken out" a bit before I upload the source - if
  37. anyone wants it.  Just let me know if I should upload it.
  38.  
  39. Jim Stout
  40.  
  41. CompuServe  : 73240,2052
  42. AppleLink   : WRQ
  43. AOL         : JasG
  44. Internet    : JimS@WRQ.COM
  45. ------------------------------------------------------------------------
  46.  
  47. A few weeks back I was reading the Human Interface volume of Inside Mac
  48. and it struck me as funny that Apple refers to various controls (used
  49. throughout the MacOS) as "not supported by the Toolbox".  Controls like
  50. "little arrow" controls used to set Date & Time, Memory etc. ,"Slider 
  51. Controls" like those in the Sound or Brightness control panels and a
  52. "Progress Bar" are simply are not to be found in the Toolbox.
  53.  
  54. Apple simulates controls by displaying appropriate PICT's and handling
  55. mouse events in a dialog filter (I think).
  56.  
  57. I realized that over the past couple of years, I had written some of
  58. those controls (the rest are hot off my coding pad).
  59.  
  60. One of the CDEF's, 104 "Tog Button" is a little different.  This is
  61. explained below in the section "About Tog Buttons".  You can also
  62. get this type of control with variation code 4 and the 3D Buttons
  63. CDEF.
  64.  
  65. All of these controls will work with System 6 or System 7 - the only
  66. caveat is that the PopUpMenu control requires PopUpMenuSelect.  I have
  67. tested them on machines from a MacPlus through a PowerMac 7100.
  68. (Actually, I've tested these as far back as System 3.2 - I've gotta
  69. do something with that original 128k Mac with MacPlus ROM's that 
  70. is sitting in the corner...)
  71.  
  72. If possible, all except the GroupBox and PopUpMenu controls will
  73. use 32bit ColorQuickdraw and offscreen GWorlds to support color.
  74. As far as I can tell, they all handle low memory situations by
  75. refusing to draw themselves.  Let me know if you see any problems.
  76.  
  77. All controls with titles honor the "useWindFont" variation code (to
  78. draw in a non-System font), will properly color themselves if a 'cctb'
  79. resource is present and when  disabled, will appear in the proper
  80. System 7 style gray (if possible).
  81.  
  82. I have not written these to support non-roman script systems.
  83.  
  84. Other variation codes, sizes and non-standard control template values
  85. are listed below.
  86.  
  87. ------------------------------------------------------------------------
  88.  
  89. The controls are:
  90.  
  91. procID    Name            Function
  92. ------    -----------        ------------------------------------------------
  93. 100        GroupBox        draws a titled box, text in upperleft
  94. 101        PopUp Menu        system 7 style popup menu control
  95. 102        Spinner            "little arrows" control
  96. 103        Date & Time        Date & Time control using "little arrows"
  97. 104        Tog Button        a new type of "one or many" control
  98. 105        HSlider            horizontal slider control
  99. 106        VSlider            vertical slider control
  100. 107        3D Buttons        a 3d replacement for the standard button CDEF
  101. 108        Progress Bar    a progress bar CDEF
  102.  
  103. Variation codes & size 
  104. ======================    
  105. (min h & w is minimum required height & width for the control rect 
  106. when using Chicago 12 font)
  107.  
  108. CDEF        varCode        min h & w    Result
  109. ---------    ----------    ----------    ------------------------------------
  110. GroupBox    0                        Draws a solid frame
  111.             1                        Draws a dotted frame
  112.             2                        * not used *
  113.             4                        * not used *
  114.             8                        Use the Window font for title
  115.             
  116.     NOTE: To avoid problems with "layering" of this control over or
  117.     under the contents of a box, set the height of this control to 16 or
  118.     so, then put the height you REALLY want in the contrlMax field of the
  119.     CNTL template.  The control rect (used by the Dialog Manager) will 
  120.     not obstruct other controls.  However, when the control draws - it 
  121.     will use the height value from the contrlMax field.
  122. ------------------------------------------------------------------------            
  123. PopUp Menu                19 x nn        (see IM VI or docs for Apple CDEF 63
  124.                         19 x 25        for "triangle only" menu
  125.                         
  126.                         NOTE: if the menu has styled text items, the 
  127.                               height may need to be greater than 19.
  128.                         Unlike the Apple CDEF 63, this control will:
  129.                                 - behave the same with System 6 & 7
  130.                                 - properly handle colored menus
  131.                         It DOES NOT support:
  132.                                 - Icons or cmd keys
  133.                                 - Styled text titles
  134. ------------------------------------------------------------------------    
  135.  
  136. Spinner        0            18 x nn        Small, as in "Date & Time" Control panel
  137.             1            25 x nn        Large, as in "Memory" Control panel
  138.             2                        * not used *
  139.             4                        * not used *
  140.             8                        Use the Window font for drawing
  141.             
  142.                         NOTE: use 18 x 11 or 25 x 15 to get arrows only
  143. ------------------------------------------------------------------------    
  144.  
  145. Date & Time    0            18 x 160    Date left, Time right justified    
  146.             1            18 x 80        Date only, left justified    
  147.             2            18 x 80        Time only, left justified    
  148.             4            37 x 80        Both on 2 lines, date over time
  149.             8                        Use the Window font for drawing
  150.                         
  151.             NOTE: non-standard date or time separator characters may   
  152.                   need a larger rect.
  153. ------------------------------------------------------------------------    
  154.                                 
  155. Tog Button    0            18 x nn        Normal button title
  156.             1                        * not used *
  157.             2                        * not used *
  158.             4                        * not used *
  159.             8                        Use the Window font for title
  160. ------------------------------------------------------------------------    
  161.                         
  162. HSlider        0            24 x 121    As in the "Brightness" Control panel
  163.             1                        "Thumb" is drawn in gray pattern
  164.             2                        Scale drawn in bg color (not filled)
  165.             4                        Scale drawn in gray pattern
  166.             8                        * not used *
  167. ------------------------------------------------------------------------    
  168.  
  169. VSlider        0            105 x 24    As in the "Sound" Control panel
  170.             1                        "Thumb" is drawn in gray pattern
  171.             2                        Scale drawn in bg color (not filled)
  172.             4                        "Thumb" will not "snap" to tick mark
  173.             8                        Scale is blank, no numbers, no marks
  174.             
  175.                         NOTE: ht is 12*divisions + 21 (see below)
  176. ------------------------------------------------------------------------    
  177.  
  178. 3D Buttons    0            any x any    draws a push button
  179.             1                        draws a checkbox
  180.             2                        draws a radiobutton
  181.             4                        draws a Tog Button control
  182.             8                        Use the Window font for title
  183.             
  184.     NOTE: This control should behave just like the standard CDEF 0 when 
  185.           running in 1 bit (B&W) mode OR if the background is white. If 
  186.           running on a non-white background, this CDEF will draw "3 D" 
  187.           controls.  Colors are taken from a 'cctb' resource.
  188. ------------------------------------------------------------------------    
  189.  
  190. Progress Bar 0            any x any    draws a horizontal progress bar
  191.              1                        draws a vertical progress bar
  192.              2                        draws a rounded, 3D progress bar
  193.              4                        not used
  194.              8                        not used
  195. --------------------------------------------------------------------    
  196.             
  197. Control Drawing
  198. ===============            
  199.                         
  200. All control drawing is clipped to the control rect, so it should be 
  201. pretty obvious if you have a control rect that is too small.
  202.  
  203. Non-standard control RefCon usage
  204. ===================================
  205.  
  206. Note: Several of these controls use the control refcon field.  They are:
  207.  
  208.     PopUp Menu     - use of refcon is identical to Apple's CDEF 63
  209.                 - not used by control after initialization.
  210.                     
  211.     Spinner        - refcon may be used to indicate a DITL item for an 
  212.                   edit item to update as the control value changes.
  213.                 - not used by control after initialization.
  214.                     
  215.     VSlider     - refcon may be used to indicate the number of divisions 
  216.                   but will default to 7 divisions.
  217.                 - not used by control after initialization.
  218.                     
  219.     DateTime    - refcon is the "value" of the control.  It is updated
  220.                   by the control.
  221.                 - ** USED ** by control after initialization - see below.
  222.     
  223. Non-standard control template usage
  224. ===================================
  225.                 
  226. GroupBox        does not use min, value or rfCon. max is used for the
  227.                 true height of the control.
  228.  
  229. PopUp Menu        (see Inside Mac VI or other docs for Apple CDEF 63)
  230.                 Does NOT support menu icons or cmd-keys, the menu
  231.                 can have them, but the control will not draw them.
  232.  
  233. Spinner            standard use of min, max and value.
  234.                 the control refCon field can be a DITL item number 
  235.                 for an editText item to update with the control value.
  236.                         
  237. Date & Time        does not use min, max
  238.                 contrlValue indicates hilited digits
  239.                 1    hours
  240.                 2    minutes
  241.                 3    AM/PM
  242.                 6    month  (6,7,8 match user order from Control Panel
  243.                 7    day        and may not have these meanings)
  244.                 8    year
  245.                                     
  246.                 contrlRfCon is seconds as passed to GetDateTime(),  
  247.                 Secs2Date() and Date2Secs() calls.  This is the "value" 
  248.                 that the control adjusts.  Use GetCRefCon() to retrieve 
  249.                 the "date time" value and SetCRefCon() to set it.
  250.                         
  251. HSlider            min = 0, max = 100 - these are set by the control.
  252.                 value returned will range from min to max
  253.  
  254. VSlider            refCon is used to indicate 'number of divisions' from 
  255.                 2-20. Default is 7 divisions with tick marks from 0-7.
  256.                 min = 0
  257.                 max = 12*"number of divisions"
  258.                         
  259.                 The value returned will range from min to max.
  260.                         
  261.                 The control "thumb" will "snap" to a tick mark.
  262.                 This means that the value will always be 12*tick mark.
  263.  
  264.                 To avoid the "snap" behavior, use varCode 4.
  265.                         
  266. 3D Buttons        standard use of all fields, but with an 
  267.                 additional variation code for "Tog" buttons.
  268. Progress Bar    standard use of all fields
  269.  
  270. -----------------------------------------------------------------------
  271. About "TogButtons" 
  272. -----------------------------------------------------------------------
  273. In early November 1991, I ran across an article in the October 1991
  274. "Apple Direct" by Bruce Tognazzini - Apple's human interface guru.  
  275. Each month Tog would write a column dealing with HI issues.  The article
  276. that caught my eye back then was titled "Case Study: One or more Buttons".  
  277.  
  278. I don't think that I can legally include Tog's article with this bit of
  279. code, but you can find it on many of the Apple Developer CD's in the 
  280.  
  281.     Periodicals:Apple Direct:Apple Direct Oct '91:Tog folder.  
  282.  
  283. It also is in his book "Tog on Interface" as chapter 36.
  284.  
  285. To summarize, Tog wrote about the design and usability testing of a new 
  286. interface element that he called "One or more Buttons" - essentially a
  287. control that was part Checkbox and part Radio button.  A group of
  288. Checkboxes can all be "Off" and a group of Radio buttons can only have
  289. one "On" value.  What was needed was a group that MUST have ONE "On"
  290. control but could have many "On" controls - a "One or more Button".
  291.  
  292. Tog's design for what I call "Tog Buttons" is a cross between a Radio
  293. button and a Checkbox.  The control is a diamond (a Checkbox rotated 90
  294. degrees) with a diamond shaped indicator (like the  circular indicator
  295. in a Radio button).  The behavior is also a cross between the two
  296. other controls.  If only one control is ON, then clicking on it turns
  297. that control OFF and turns on an adjacent control (just like a pair of
  298. radio buttons), if one or more of the controls is ON, then others can be
  299. toggled (just like a checkbox).
  300.  
  301. Another twist on the behavior is that if only one control is ON, a
  302. click on that control will turn if OFF, and turn on the one just
  303. above (or to the left of) it.  When the first (or last) control 
  304. is reached, the direction reverses, turning on the one just below 
  305. (or to the right).  Tog likened this behavior to that of a drop of 
  306. mercury when you press on it with a finger (but without the toxicity!).
  307.  
  308. The behavior is such that with a single ON button, clicking on just
  309. that button, causes it to "jump" up (or leftward).  If you keep
  310. chasing the single ON button, it will keep "jumping" up until it must
  311. reverse direction and start "jumping" down.  Unless you are "chasing"
  312. a button like this, when only one button is ON, the default behavior
  313. is to jump UP (or to the left).  To see this, try the TogDemo program
  314. and you will see what I mean.   
  315.  
  316. For some reason, this appealed to me as an interesting project and I
  317. spent a fun afternoon implementing Tog's idea.  I ended up creating a
  318. CDEF to implement the control described by Tog and a couple of routines
  319. in C to support the desired behavior of "Tog Buttons".
  320.  
  321. -----------------------------------------------------------------------
  322. How to use a Tog Button in a dialog
  323. ===================================
  324.  
  325. Obviously, using a Tog Button is only needed if you have a situation
  326. that has a group of items that requires "One or Many" values.  An
  327. example might be a search :
  328.  
  329.    - Search -----------------
  330.   |                          |
  331.   | <•> C files (.c)         |
  332.   | < > Include files (.h)   |
  333.   | < > Resource files (.r)  |
  334.   |                          |
  335.    --------------------------
  336.    
  337. The file cdefDemo.c shows how to use "Tog Buttons", but here is a
  338. brief list.
  339.  
  340. 1. Include the files TogLib.c/TogLib.h in your project.
  341. 2. Define the group of "Tog Buttons" in your dialog resource.
  342. 3. DITL item numbers MUST be successively numbered for
  343.    each group of "Tog Buttons".  This is IMPORTANT!
  344. 4. After calling GetNewDialog, but before calling ModalDialog, 
  345.    call initTogButtons() for each group of "Tog Buttons".
  346. 5. When a "Tog Button" is clicked, call setTogButtons() for
  347.    that group of "Tog Buttons".
  348. 6. When a non-Tog Button control is clicked, do whatever is
  349.    needed, then call resetTogButtons() for each group of 
  350.    "Tog Buttons".  This step simply insures that the next
  351.    click on a "Tog Button" will "go up".
  352. 7. Use regular calls to GetDItem() and GetCtlValue() to retrieve
  353.    the control values for "Tog Buttons".
  354.  
  355. The TogLib routines are the key to maintaining consistent behavior
  356. of the "Tog Buttons".  TogLib.h has a short description of each
  357. routine.
  358. ----------------------------------------------------------------------*/